<!--
 * @Description: 单品等叫功能
 * @Author: duan_shuangjun 丶
 * @Date: 2025-09-10 14:25:30
-->
<template>
  <custom-dialog v-model:visible="dialogVisible">
    <div class="dialog-content">
      <div class="title">菜品等叫</div>
      <div class="description" v-if="selectedFood">
        确定对（{{ selectedFood.name }}）等叫，并远程打印等叫通知单吗？
      </div>
      <div class="description-box">
        <div class="label">等叫提醒时间</div>
        <el-time-picker
          class="date-picker-select"
          v-model="waitTime"
          format="HH:mm"
          value-format="HH:mm:ss"
          placeholder="选择等叫时间"
          style="width: 1.9rem"
        >
        </el-time-picker>
      </div>

      <div class="btn-box">
        <el-button
          class="cancel-btn"
          @click="handleConfirm(0)"
          :loading="loading && printType === 0"
        >
          {{ loading && printType === 0 ? '处理中...' : '只等叫' }}
        </el-button>
        <el-button
          class="confirm-btn"
          @click="handleConfirm(1)"
          :loading="loading && printType === 1"
        >
          {{ loading && printType === 1 ? '处理中...' : '等叫并打印' }}
        </el-button>
      </div>
    </div>
  </custom-dialog>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { orderFoodWait } from '@/api/food'
import CustomDialog from '@/components/CustomDialog/Index.vue'
import { ElButton } from 'element-plus'

interface Props {
  deskDetail: any
  selectFoodActiveIndex: number
  modelValue: boolean
}

interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'refreshData'): void
  (e: 'refreshDesk'): void
}

const props = withDefaults(defineProps<Props>(), {
  modelValue: false,
  deskDetail: () => ({}),
  selectFoodActiveIndex: -1
})

const emit = defineEmits<Emits>()

// 对话框显示状态
const dialogVisible = computed({
  get: () => props.modelValue,
  set: val => emit('update:modelValue', val)
})
// 等叫时间
const waitTime = ref<string | null>(null)
// 选中的菜品
const selectedFood = ref<any>(null)
// 加载状态
const loading = ref(false)
// 打印类型（0：只等叫，1：等叫并打印）
const printType = ref(-1)

// 监听对话框打开状态
watch(
  () => dialogVisible.value,
  val => {
    if (val) {
      debugger
      // 获取选中的菜品信息
      selectedFood.value = props.deskDetail.foodList?.[props.selectFoodActiveIndex]
      // 重置数据
      waitTime.value = null
    }
  }
)

// 确认等叫处理
const handleConfirm = async (print: number) => {
  //   if (!waitTime.value) {
  //     ElMessage.warning('请先选择等叫提醒时间')
  //     return
  //   }

  if (loading.value) return

  try {
    loading.value = true
    printType.value = print

    const params = {
      id: selectedFood.value.id,
      wait_time: waitTime.value,
      print: print // 0: 只等叫, 1: 等叫并打印
    }

    const res = await orderFoodWait(params)
    if (res.status === 200) {
      ElMessage.success(res.data?.message || (print === 1 ? '等叫并打印成功' : '等叫设置成功'))
      dialogVisible.value = false
      emit('refreshData')
      emit('refreshDesk')
    } else {
      ElMessage.error(res.data?.message || '等叫设置失败')
    }
  } catch (error) {
    console.error('等叫设置失败:', error)
    ElMessage.error('等叫设置失败')
  } finally {
    loading.value = false
    printType.value = -1
  }
}
</script>

<style lang="scss" scoped>
.dialog-content {
  .title {
    font-size: 20px;
    font-family:
      PingFang SC,
      PingFang SC-Bold;
    font-weight: 700;
    text-align: center;
    color: #333333;
  }

  .description {
    margin-top: 70px;
    font-size: 20px;
    font-family:
      PingFang SC,
      PingFang SC-Regular;
    font-weight: 400;
    text-align: center;
    color: #353535;
  }

  .description-box {
    margin-top: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 48px;
    font-size: 16px;
    font-family:
      PingFang SC,
      PingFang SC-Regular;
    font-weight: 400;
    text-align: center;
    color: #333333;

    .label {
      margin-right: 20px;
    }

    .date-picker-select {
      width: 420px;
    }
  }

  .btn-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 45px;

    .cancel-btn {
      width: 190px;
      height: 60px;
      background: #ffffff;
      border: 1px solid #333333;
      border-radius: 12px;
      font-size: 18px;
      font-family:
        PingFang SC,
        PingFang SC-Regular;
      font-weight: 400;
      text-align: center;
      color: #333333;
      line-height: 60px;
      cursor: pointer;

      &.loading {
        opacity: 0.7;
        cursor: not-allowed;
      }
    }

    .confirm-btn {
      width: 190px;
      height: 60px;
      background: #7c0a03;
      border-radius: 12px;
      font-size: 18px;
      font-family:
        PingFang SC,
        PingFang SC-Regular;
      font-weight: 400;
      text-align: center;
      color: #ffffff;
      line-height: 60px;
      cursor: pointer;

      &.loading {
        opacity: 0.7;
        cursor: not-allowed;
      }
    }
  }
}
</style>
